package Dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.ItemDao;
import pojo.Item;
import util.jdbc_mysql;

public class ItemDaoImpl implements ItemDao {

	@Override
	public int addItem(Item item) {
		int rs = 0;
		String sql = "insert into item (item_name,place,create_time,details,down)" + "values(?,?,?,?,?)";
		Connection con = null;
		PreparedStatement pst = null;
		con = jdbc_mysql.getConn();
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, item.getItemName());
			pst.setString(2, item.getPlace());
			pst.setString(3, item.getItemTime());
			pst.setString(4, item.getDetails());
			pst.setBoolean(5, item.isDown());
			rs = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(con, pst, null);
		}
		return rs;
	}

	@Override
	public int deleteItem(int itemId) {
		int rs = 0;
		String sql = "delete from item where item_id=?";
		Connection con = null;
		PreparedStatement pst = null;
		con = jdbc_mysql.getConn();
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, itemId);
			rs = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(con, pst, null);
		}

		return rs;
	}

	@Override
	public Item findItem(String itemName) {
		Item item = new Item();
		ResultSet rs = null;
		PreparedStatement pStatement = null;
		String sql = "select * from users where item_name=?";
		Connection cnConnection = jdbc_mysql.getConn();
		try {
			pStatement = cnConnection.prepareStatement(sql);
			pStatement.setString(1, itemName);
		
			rs = pStatement.executeQuery();
			if (rs.next()) {
				item.setDetails(rs.getString("details"));
				item.setDown(rs.getBoolean("down"));
				item.setItemId(rs.getInt("item_id"));
				item.setItemName(rs.getString("item_name"));
				item.setItemTime(rs.getString("create_time"));
				item.setPlace(rs.getString("place"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(cnConnection, pStatement, rs);
		}
		return item;

	}

	@Override
	public List<Item> findAllItems() {
		List<Item> items = new ArrayList<Item>();
		String sql = "select * from item";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = jdbc_mysql.getConn();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				Item item = new Item();
				item.setItemId(rs.getInt("item_id"));
				item.setItemName(rs.getString("item_name"));
				item.setItemTime(rs.getString("create_time"));
				item.setPlace(rs.getString("place"));
				item.setDetails(rs.getString("details"));
				item.setDown(rs.getBoolean("down"));
				items.add(item);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(con, pst, rs);
		}
		return items;
	}

	@Override
	public int updateItem(Item item) {
		int rs =0;
		String sql="delete from item where item_id=?";
		Connection con=null;
		PreparedStatement pst=null;
		con =jdbc_mysql.getConn();
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, item.getItemId());
			rs=pst.executeUpdate();
			System.out.println("idi135");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String sql2 = "insert into item (item_id,item_name,place,create_time,details,down)" + "values(?,?,?,?,?,?)";
		Connection con2 = null;
		PreparedStatement pst2 = null;
		con2 = jdbc_mysql.getConn();
		try {
			pst = con.prepareStatement(sql2);
			pst.setInt(1, item.getItemId());
			pst.setString(2, item.getItemName());
			pst.setString(3, item.getPlace());
			pst.setString(4, item.getItemTime());
			pst.setString(5, item.getDetails());
			pst.setBoolean(6, item.isDown());
			rs = pst.executeUpdate();
			System.out.println("idi153");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(con, pst, null);
			jdbc_mysql.close(con2, pst2, null);
		}
		return rs;
	    // TODO Auto-generated method stub
		

	}

	@Override
	public Item lookItem(int itemId) {
		Item item = new Item();
		ResultSet rs = null;
		PreparedStatement pStatement = null;
		String sql = "select * from item where item_id=?";
		Connection cnConnection = jdbc_mysql.getConn();
		try {
			pStatement = cnConnection.prepareStatement(sql);
			pStatement.setInt(1, itemId);
		
			rs = pStatement.executeQuery();
			if (rs.next()) {
				item.setDetails(rs.getString("details"));
				item.setDown(rs.getBoolean("down"));
				item.setItemId(rs.getInt("item_id"));
				item.setItemName(rs.getString("item_name"));
				item.setItemTime(rs.getString("create_time"));
				item.setPlace(rs.getString("place"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jdbc_mysql.close(cnConnection, pStatement, rs);
		}
		return item;
	}

}
